/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "zobjects_gtk.h"

static int guiedit_Create( struct ZlangRuntime *rt , struct ZlangObject *guiwindow_obj , struct ZlangObject *out1 , int32_t max_show_len , int32_t max_len , char *str )
{
	struct ZlangDirectProperty_guiedit		*guiedit_direct_prop = GetObjectDirectProperty(out1) ;
	struct ZlangDirectProperty_guiwidget_common	*guiedit_common = & (guiedit_direct_prop->common) ;

	EVAL_GTK_WIDGET_NEW_IN_CREATE( guiedit_common->gtk_widget , gtk_entry_new() )
	SET_CONTROL_DEFAULT_STYLE( guiwindow_obj , guiedit_common->gtk_widget )
	if( max_show_len > 0 )
		g_object_set( GTK_ENTRY(guiedit_common->gtk_widget) , "width-chars" , max_show_len , NULL );
	if( max_len > 0 )
		gtk_entry_set_max_length( GTK_ENTRY(guiedit_common->gtk_widget) , max_len );
	if( str )
		gtk_editable_set_text( GTK_EDITABLE(guiedit_common->gtk_widget) , str );

	TRANSFER_INPUT_OBJECT_TO_NEW_OBJECT( guiedit_common->guiwindow_obj , guiwindow_obj )

	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guiedit_Create_guiwindow;
int ZlangInvokeFunction_guiedit_Create_guiwindow( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;

	return guiedit_Create( rt , in1 , out1 , -1 , -1 , NULL );
}

ZlangInvokeFunction ZlangInvokeFunction_guiedit_Create_guiwindow_string;
int ZlangInvokeFunction_guiedit_Create_guiwindow_string( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*in2 = GetInputParameterInLocalObjectStack(rt,2) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	char						*str = NULL ;

	CallRuntimeFunction_string_GetStringValue( rt , in2 , & str , NULL );

	return guiedit_Create( rt , in1 , out1 , 0 , 0 , str );
}

ZlangInvokeFunction ZlangInvokeFunction_guiedit_Create_guiwindow_int;
int ZlangInvokeFunction_guiedit_Create_guiwindow_int( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*in2 = GetInputParameterInLocalObjectStack(rt,2) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	int32_t						max_show_len ;

	CallRuntimeFunction_int_GetIntValue( rt , in2 , & max_show_len );

	return guiedit_Create( rt , in1 , out1 , max_show_len , -1 , NULL );
}

ZlangInvokeFunction ZlangInvokeFunction_guiedit_Create_guiwindow_int_int;
int ZlangInvokeFunction_guiedit_Create_guiwindow_int_int( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*in2 = GetInputParameterInLocalObjectStack(rt,2) ;
	struct ZlangObject				*in3 = GetInputParameterInLocalObjectStack(rt,3) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	int32_t						max_show_len ;
	int32_t						max_len ;

	CallRuntimeFunction_int_GetIntValue( rt , in2 , & max_show_len );
	CallRuntimeFunction_int_GetIntValue( rt , in3 , & max_len );

	return guiedit_Create( rt , in1 , out1 , max_show_len , max_len , NULL );
}

ZlangInvokeFunction ZlangInvokeFunction_guiedit_Create_guiwindow_int_int_string;
int ZlangInvokeFunction_guiedit_Create_guiwindow_int_int_string( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*in2 = GetInputParameterInLocalObjectStack(rt,2) ;
	struct ZlangObject				*in3 = GetInputParameterInLocalObjectStack(rt,3) ;
	struct ZlangObject				*in4 = GetInputParameterInLocalObjectStack(rt,4) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	int32_t						max_show_len ;
	int32_t						max_len ;
	char						*str = NULL ;

	CallRuntimeFunction_int_GetIntValue( rt , in2 , & max_show_len );
	CallRuntimeFunction_int_GetIntValue( rt , in3 , & max_len );
	CallRuntimeFunction_string_GetStringValue( rt , in4 , & str , NULL );

	return guiedit_Create( rt , in1 , out1 , max_show_len , max_len , str );
}

ZlangInvokeFunction ZlangInvokeFunction_guiedit_SetPasswordMode_bool;
int ZlangInvokeFunction_guiedit_SetPasswordMode_bool( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guiedit	*guiedit_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangObject			*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject			*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	unsigned char				b ;
	
	CallRuntimeFunction_bool_GetBoolValue( rt , in1 , & b );

	gtk_entry_set_visibility( GTK_ENTRY(guiedit_direct_prop->common.gtk_widget) , b );
	
	TRANSFER_MASTER_OBJECT_TO_OUTPUT_OBJECT
	
	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guiedit_SetPlaceHolder_string;
int ZlangInvokeFunction_guiedit_SetPlaceHolder_string( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guiedit	*guiedit_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangObject			*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject			*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	char					*str = NULL ;
	
	CallRuntimeFunction_string_GetStringValue( rt , in1 , & str , NULL );

	gtk_entry_set_placeholder_text( GTK_ENTRY(guiedit_direct_prop->common.gtk_widget) , str );
	
	TRANSFER_MASTER_OBJECT_TO_OUTPUT_OBJECT
	
	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guiedit_SetShowLength_int;
int ZlangInvokeFunction_guiedit_SetShowLength_int( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guiedit	*guiedit_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangObject			*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject			*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	int32_t					len ;
	
	CallRuntimeFunction_int_GetIntValue( rt , in1 , & len );

	g_object_set( GTK_ENTRY(guiedit_direct_prop->common.gtk_widget) , "width-chars" , len , NULL );
	
	TRANSFER_MASTER_OBJECT_TO_OUTPUT_OBJECT
	
	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guiedit_SetMaxLength_int;
int ZlangInvokeFunction_guiedit_SetMaxLength_int( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guiedit	*guiedit_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangObject			*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject			*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	int32_t					len ;
	
	CallRuntimeFunction_int_GetIntValue( rt , in1 , & len );

	gtk_entry_set_max_length( GTK_ENTRY(guiedit_direct_prop->common.gtk_widget) , len );
	
	TRANSFER_MASTER_OBJECT_TO_OUTPUT_OBJECT
	
	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guiedit_SetText_string;
int ZlangInvokeFunction_guiedit_SetText_string( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guiedit	*guiedit_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangObject			*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject			*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	char					*str = NULL ;
	
	CallRuntimeFunction_string_GetStringValue( rt , in1 , & str , NULL );

	gtk_editable_set_text( GTK_EDITABLE(guiedit_direct_prop->common.gtk_widget) , str );
	
	TRANSFER_MASTER_OBJECT_TO_OUTPUT_OBJECT
	
	return 0;
}

ZLANG_INVOKE_FUNCTION_guiwidget_all_common_functions( edit )

ZLANG_INVOKE_FUNCTION_guictl_all_common_functions( edit )

ZlangInvokeFunction ZlangInvokeFunction_guiedit_GetText;
int ZlangInvokeFunction_guiedit_GetText( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guiedit	*guiedit_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangObject			*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	GtkEntryBuffer				*gtk_entry_buf = NULL ;
	char					*str = NULL ;

	gtk_entry_buf = gtk_entry_get_buffer( GTK_ENTRY(guiedit_direct_prop->common.gtk_widget) ) ;
	str = (char *)gtk_entry_buffer_get_text( gtk_entry_buf ) ;
	CallRuntimeFunction_string_SetStringValue( rt , out1 , str , STRLEN_OF_STRING );

	return 0;
}

ZlangCreateDirectPropertyFunction ZlangCreateDirectProperty_guiedit;
void *ZlangCreateDirectProperty_guiedit( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guiedit		*guiedit_prop = NULL ;
	struct ZlangDirectProperty_guiwidget_common	*guiedit_common = NULL ;
	
	guiedit_prop = (struct ZlangDirectProperty_guiedit *)ZLMALLOC( sizeof(struct ZlangDirectProperty_guiedit) ) ;
	if( guiedit_prop == NULL )
	{
		SET_RUNTIME_ERROR( rt , RUNTIME_ERROR , ZLANG_ERROR_ALLOC , "alloc memory for entity" )
		return NULL;
	}
	memset( guiedit_prop , 0x00 , sizeof(struct ZlangDirectProperty_guiedit) );

	guiedit_common = (struct ZlangDirectProperty_guiwidget_common *)guiedit_prop ;
	guiedit_common->gtk_widget_type_id = GTK_TYPE_ENTRY ;

	INIT_ZLANGDIRECTPROPERTY_guiwidget_common(guiedit)
	
	return guiedit_prop;
}

ZlangDestroyDirectPropertyFunction ZlangDestroyDirectProperty_guiedit;
void ZlangDestroyDirectProperty_guiedit( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guiedit	*guiedit_direct_prop = GetObjectDirectProperty(obj) ;
	
	CLEAN_ZLANGDIRECTPROPERTY_guiwidget_common(guiedit)

	ZLFREE( guiedit_direct_prop );
	
	return;
}

ZlangSummarizeDirectPropertySizeFunction ZlangSummarizeDirectPropertySize_guiedit;
void ZlangSummarizeDirectPropertySize_guiedit( struct ZlangRuntime *rt , struct ZlangObject *obj , size_t *summarized_obj_size , size_t *summarized_direct_prop_size )
{
	SUMMARIZE_SIZE( summarized_direct_prop_size , sizeof(struct ZlangDirectProperty_guiedit) )
	return;
}

static struct ZlangDirectFunctions direct_funcs_guiedit =
	{
		ZLANG_OBJECT_guiedit , /* char *ancestor_name */
		
		ZlangCreateDirectProperty_guiedit , /* ZlangCreateDirectPropertyFunction *create_entity_func */
		ZlangDestroyDirectProperty_guiedit , /* ZlangDestroyDirectPropertyFunction *destroy_entity_func */
		
		NULL , /* ZlangFromCharPtrFunction *from_char_ptr_func */
		NULL , /* ZlangToStringFunction *to_string_func */
		NULL , /* ZlangFromDataPtrFunction *from_data_ptr_func */
		NULL , /* ZlangGetDataPtrFunction *get_data_ptr_func */
		
		NULL , /* ZlangOperatorFunction *oper_PLUS_func */
		NULL , /* ZlangOperatorFunction *oper_MINUS_func */
		NULL , /* ZlangOperatorFunction *oper_MUL_func */
		NULL , /* ZlangOperatorFunction *oper_DIV_func */
		NULL , /* ZlangOperatorFunction *oper_MOD_func */
		
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_NEGATIVE_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_NOT_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_BIT_REVERSE_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_PLUS_PLUS_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_MINUS_MINUS_func */
		
		NULL , /* ZlangCompareFunction *comp_EGUAL_func */
		NULL , /* ZlangCompareFunction *comp_NOTEGUAL_func */
		NULL , /* ZlangCompareFunction *comp_LT_func */
		NULL , /* ZlangCompareFunction *comp_LE_func */
		NULL , /* ZlangCompareFunction *comp_GT_func */
		NULL , /* ZlangCompareFunction *comp_GE_func */
		
		NULL , /* ZlangLogicFunction *logic_AND_func */
		NULL , /* ZlangLogicFunction *logic_OR_func */
		
		NULL , /* ZlangLogicFunction *bit_AND_func */
		NULL , /* ZlangLogicFunction *bit_XOR_func */
		NULL , /* ZlangLogicFunction *bit_OR_func */
		NULL , /* ZlangLogicFunction *bit_MOVELEFT_func */
		NULL , /* ZlangLogicFunction *bit_MOVERIGHT_func */
		
		ZlangSummarizeDirectPropertySize_guiedit , /* ZlangSummarizeDirectPropertySizeFunction *summarize_direct_prop_size_func */
	} ;

ZlangImportObjectFunction ZlangImportObject_guiedit;
struct ZlangObject *ZlangImportObject_guiedit( struct ZlangRuntime *rt )
{
	struct ZlangObject	*obj = NULL ;
	struct ZlangFunction	*func = NULL ;
	int			nret = 0 ;
	
	nret = ImportObject( rt , & obj , ZLANG_OBJECT_guiedit , & direct_funcs_guiedit , sizeof(struct ZlangDirectFunctions) , NULL ) ;
	if( nret )
	{
		SET_RUNTIME_ERROR( rt , RUNTIME_ERROR , ZLANG_ERROR_LINK_FUNC_TO_ENTITY , "import object to global objects heap" )
		return NULL;
	}
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guiedit_Create_guiwindow , ZLANG_OBJECT_guiedit , "Create" , ZLANG_OBJECT_guiwindow,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guiedit_Create_guiwindow_string , ZLANG_OBJECT_guiedit , "Create" , ZLANG_OBJECT_guiwindow,NULL , ZLANG_OBJECT_string,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guiedit_Create_guiwindow_int , ZLANG_OBJECT_guiedit , "Create" , ZLANG_OBJECT_guiwindow,NULL , ZLANG_OBJECT_int,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guiedit_Create_guiwindow_int_int , ZLANG_OBJECT_guiedit , "Create" , ZLANG_OBJECT_guiwindow,NULL , ZLANG_OBJECT_int,NULL , ZLANG_OBJECT_int,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guiedit_Create_guiwindow_int_int_string , ZLANG_OBJECT_guiedit , "Create" , ZLANG_OBJECT_guiwindow,NULL , ZLANG_OBJECT_int,NULL , ZLANG_OBJECT_int,NULL , ZLANG_OBJECT_string,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guiedit_SetPasswordMode_bool , ZLANG_OBJECT_guiedit , "SetPasswordMode" , ZLANG_OBJECT_bool,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guiedit_SetPlaceHolder_string , ZLANG_OBJECT_guiedit , "SetPlaceHolder" , ZLANG_OBJECT_string,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guiedit_SetShowLength_int , ZLANG_OBJECT_guiedit , "SetShowLength" , ZLANG_OBJECT_int,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guiedit_SetMaxLength_int , ZLANG_OBJECT_guiedit , "SetMaxLength" , ZLANG_OBJECT_int,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guiedit_SetText_string , ZLANG_OBJECT_guiedit , "SetText" , ZLANG_OBJECT_string,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	ADD_FUNCTION_AND_PARAMETERS_IN_OBJECT_guiwidget_all_common_functions( edit )

	ADD_FUNCTION_AND_PARAMETERS_IN_OBJECT_guictl_all_common_functions( edit )

	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guiedit_GetText , ZLANG_OBJECT_string , "GetText" , NULL ) ;
	if( func == NULL )
		return NULL;

	return obj ;
}

